home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Workspace / Locus / Source / DynamicItems.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  262 lines

  1.  
  2. /*
  3.     Copyright 1993  Jeremy Slade.  All rights reserved.
  4. */
  5.  
  6. #import "DynamicItems.h"
  7.  
  8. #import "DynamicItemSpec.h"
  9. #import "Globals.h"
  10. #import "ItemCell.h"
  11.  
  12.  
  13. // DynamicItem update handler
  14. void    updateHandler ( DPSTimedEntry teNum, double now, Group *self );
  15.  
  16.  
  17. @implementation Group ( DynamicItems )
  18.  
  19.  
  20. // -------------------------------------------------------------------------
  21. //   DynamicItemSpecs
  22. // -------------------------------------------------------------------------
  23.  
  24.  
  25. - dynamicItemSpecs
  26. /*
  27.     Returns a List containing all the dynamic items (instances of DynamicItemSpec) for this group.
  28. */
  29. {
  30.     return ( dynamicItemSpecs );
  31. }
  32.  
  33.  
  34.  
  35. - dynamicItemSpecMatching:(const char *)itemSpec
  36. /*
  37.     Search the dynamicItem list for an object matching itemSpec and return it
  38. */
  39. {
  40.     int i, count;
  41.     id    spec;
  42.     
  43.     count = [dynamicItemSpecs count];
  44.     for ( i=0; i<count; i++ ) {
  45.         if ( !strcmp ( [(spec = [dynamicItemSpecs objectAt:i]) expression],
  46.             itemSpec ) )
  47.                 return ( spec );
  48.     }
  49.     
  50.     return ( nil ); // No match found
  51. }
  52.  
  53.  
  54.  
  55. - addDynamicItemSpec:(const char *)itemSpec
  56. /*
  57.     Add itemSpec to the dynamicItemSpecs list
  58. */
  59. {
  60.     if ( ![self dynamicItemSpecMatching:itemSpec] ) {
  61.         [dynamicItemSpecs addObject:
  62.             [[DynamicItemSpec alloc] initExpr:itemSpec]];
  63.         [self setChanged:YES];
  64.     }
  65.     
  66.     return ( self );
  67. }
  68.  
  69.  
  70.  
  71. - removeDynamicItemSpec:(const char *)itemSpec
  72. /*
  73.     Remove itemSpec from the dynamicItemSpecs list
  74. */
  75. {
  76.     id spec;
  77.     
  78.     if ( spec = [self dynamicItemSpecMatching:itemSpec] ) {
  79.         [dynamicItemSpecs removeObject:spec];
  80.         [self setChanged:YES];
  81.     }
  82.     
  83.     return ( self );
  84. }
  85.  
  86.  
  87.  
  88. - enableDynamicItemSpec:(const char *)itemSpec
  89. /*
  90.     Enable the dynamicItemSpec matching itemSpec
  91. */
  92. {
  93.     id spec;
  94.  
  95.     if ( spec = [self dynamicItemSpecMatching:itemSpec] ) {
  96.         [spec setEnabled:YES];
  97.         [self setChanged:YES];
  98.     }
  99.     
  100.     return ( self );
  101. }
  102.  
  103.  
  104.  
  105. - disableDynamicItemSpec:(const char *)itemSpec
  106. /*
  107.     Disable the dynamicItemSpec matching itemSpec
  108. */
  109. {
  110.     id spec;
  111.  
  112.     if ( spec = [self dynamicItemSpecMatching:itemSpec] ) {
  113.         [spec setEnabled:NO];
  114.         [self setChanged:YES];
  115.     }
  116.  
  117.     return ( self );
  118. }
  119.  
  120.  
  121.  
  122. // -------------------------------------------------------------------------
  123. //   Updating DynamicItems
  124. // -------------------------------------------------------------------------
  125.  
  126.  
  127. - setUpdateInterval:(float)interval
  128. {
  129.     [self stopDynamicUpdate];
  130.     if ( dynamicUpdateInterval != interval ) {
  131.         dynamicUpdateInterval = interval;
  132.         [self setChanged:YES];
  133.     }
  134.     [self startDynamicUpdate];
  135.     
  136.     return ( self );
  137. }
  138.  
  139.  
  140.  
  141. - (float)updateInterval
  142. {
  143.     return ( dynamicUpdateInterval );
  144. }
  145.  
  146.  
  147.  
  148. - startDynamicUpdate
  149. {
  150.     [self stopDynamicUpdate];
  151.  
  152.     // Do this automatically when the updating starts
  153.     [self perform:@selector(updateDynamicItems:) with:self         afterDelay:0 cancelPrevious:YES];
  154.     
  155.     if ( dynamicUpdateInterval == 0.0 )
  156.         return ( self );
  157.      updateTE = DPSAddTimedEntry ( dynamicUpdateInterval,
  158.         (DPSTimedEntryProc)updateHandler, (void *)self, NX_BASETHRESHOLD+1 );
  159.  
  160.     return ( self );
  161. }
  162.  
  163.  
  164.  
  165. - stopDynamicUpdate
  166. {
  167.     if ( updateTE ) {
  168.         DPSRemoveTimedEntry ( updateTE );
  169.         updateTE = 0;
  170.     }
  171.     
  172.     return ( self );
  173. }
  174.     
  175.  
  176.  
  177. - updateDynamicItems:sender
  178. /*
  179.     Called by the update timed-entry, or manually thorugh a menu item.  Tell all of the DynamicItemSpecs to update...
  180. */
  181. {
  182. //if ( DEBUGGING ) printf ( "Performing DynamicUpdate...\n" );
  183.     [self removeDynamicItems];
  184.     [dynamicItemSpecs
  185.         makeObjectsPerform:@selector(addMatchingItemsToGroup:)
  186.         with:self];
  187.     if ( [self doesSortItems] ) {
  188.         BOOL wasChanged = [self isChanged];
  189.         [self sortItems];
  190.         [self setChanged:wasChanged];
  191.     }
  192. //if ( DEBUGGING ) printf ( "    Done.\n" );
  193.     return ( self );
  194. }
  195.  
  196.  
  197.  
  198. // -------------------------------------------------------------------------
  199. //   Working with DynamicItems
  200. // -------------------------------------------------------------------------
  201.  
  202.  
  203. - addDynamicItem:item
  204. /*
  205.     Add the DynamicItem to the group, but don't mark as changed.
  206. */
  207. {
  208.     if ( ![self itemExists:[item path]] && [self isAllowedType:[item path]] ) {
  209.         [self addObject:item];
  210.         [folder setNeedsLoadBrowser:YES];
  211.         return ( self );
  212.     } else
  213.         return ( nil );
  214. }
  215.  
  216.  
  217.  
  218. - removeDynamicItems
  219. /*
  220.     Search throug the group and remove all DynamicItems
  221. */
  222. {
  223.     register int i;
  224.     register id item;
  225.     
  226.     i = numElements;
  227.     while ( i-- ) if ( [(item = [self objectAt:i]) isDynamic] ) [self removeObjectAt:i];
  228.     return ( self );
  229. }
  230.  
  231.  
  232.  
  233.  
  234. - (int)countDynamicItems
  235. /*
  236.     Search through the group and count all DynamicItems
  237. */
  238. {
  239.     register int i, count;
  240.     
  241.     i = numElements;
  242.     count = 0;
  243.     while ( i-- ) if ( [[self objectAt:i] isDynamic] ) count++;
  244.     return ( count );
  245. }
  246.  
  247.  
  248.  
  249.  
  250. @end
  251.  
  252.  
  253.  
  254. // DynamicItem update handler
  255. void    updateHandler ( DPSTimedEntry teNum,
  256.     double now, Group *self )
  257. {
  258.     [self updateDynamicItems:self];
  259.     return;
  260. }
  261.     
  262.